Lexical Syntax

Xbase comes with a small set of lexer rules, which can be overridden and hence changed by users. However the default implementation is carefully chosen and it is recommended to stick with the lexical syntax described in the following.

Identifiers

Identifiers are used to name all constructs, such as types, methods and variables. Xbase uses the default Identifier-Syntax from Xtext - compared to Java, they are slightly simplified to match the common cases while having less ambiguities. They start with a letter a-z, A-Z or an underscore followed by more of these characters or a digit 0-9.

Escaped Identifiers

Identifiers may not have the same spelling as any reserved keyword. However, identifiers starting with a ^ are so called escaped identifiers. Escaped identifiers are used in cases when there is a conflict with a reserved keyword. Imagine you have introduced a keyword service in your language but want to call a Java property service at some point. In such cases you use an escaped identifier ^service to reference the Java property.

Syntax

terminal ID: 
    '^'? ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;

Examples

String Literals

String literals can either use single quotes (') or double quotes (") as their terminals. When using double quotes all literals allowed by Java string literals are supported. In addition new line characters are allowed, that is in Xbase all string literals can span multiple lines. When using single quotes the only difference is that single quotes within the literal have to be escaped and double quotes do not.

See ¤ 3.10.5 String Literals

In contrast to Java, equal string literals within the same class do not neccessarily refer to the same instance at runtime.

Syntax

//TODO

Examples

Integer Literals

Integer literals consist of one or more digits. Only decimal literals are supported and they always result in a value of type java.lang.Integer (it might result in native type int when translated to Java, see Types). The compiler makes sure that only numbers between 0 and Integer.MAX (0x7fffffff) are used.

There is no negative integer literal, instead the expression -23 is parsed as the prefix operator - applied to an integer literal.

Syntax

terminal INT returns ecore::EInt: 
    ('0'..'9')+
;

Comments

Xbase comes with two different kinds of comments: Single-line comments and multi-line comments. The syntax is the same as the one known from Java (see ¤ 3.7 Comments).

Syntax

terminal ML_COMMENT    : 
    '/*' -> '*/'
;
terminal SL_COMMENT : 
    '//' !('\n'|'\r')* ('\r'? '\n')?
;

White Space

The white space characters ' ' , '\t' , '\n' , and '\r are allowed to occur anywhere between the other syntactic elements.

Reserved Keywords

The following list of words are reserved keywords, thus reducing the set of possible identifiers:

  1. extends

  2. super

  3. instanceof

  4. as

  5. new

  6. null

  7. false

  8. true

  9. val

  10. var

  11. if

  12. else

  13. switch

  14. case

  15. default

  16. do

  17. while

  18. for

  19. typeof

  20. throw

  21. try

  22. catch

  23. finally

However, in case some of the keywords have to be used as identifiers, the escape character for identifiers comes in handy.